package com.liuyu.common.framework.power3.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.liuyu.commmon.framework.api.service.facade.Power3ExtendService;
import com.liuyu.commmon.framework.common.utils.PowerUtil;
import com.liuyu.common.framework.common.service.security.vo.AddPowerVo;
import com.liuyu.common.framework.common.service.security.vo.ApiPowerResult;
import com.liuyu.common.framework.power.model.RequestInfoWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

/**
 * 抽象类
 * @author: liuyu
 * @date: 2021/10/15 11:36
 * @version: 1
 */
public abstract class AbstractPower3ExtendServiceImpl implements Power3ExtendService {
    private static final String USER_CODE = "userCode";
    private static final String CLIENT_ID = "clientId";
    private static final String COM_CODE = "comCode";
    public static final String POWER_IGNORE = "power_ignore";

    @Override
    public List<String> getTaskCodes(String userCode, String appKey, String comCode) {
        return Lists.newArrayListWithExpectedSize(0);
    }

    /**
     * 获取请求阐述信息
     * @author liuyu
     * @date 2021/10/15 11:49
     * @return com.liuyu.common.framework.power3.model.RequestInfo
     */
    protected RequestInfoWrapper getRequestInfoWrapper() {
        RequestAttributes req = RequestContextHolder.getRequestAttributes();
        if (req == null) {
            throw new RuntimeException("没有用户信息");
        }
        //处理request
        HttpServletRequest request = ((ServletRequestAttributes) req).getRequest();
        //employeeId
        String userCode = request.getHeader(USER_CODE);
        //userId
        String clientId = request.getHeader(CLIENT_ID);
        String comCode = request.getHeader(COM_CODE);
//        boolean ignore = (boolean) request.getAttribute(POWER_IGNORE);
        RequestInfoWrapper requestInfoWrapper = new RequestInfoWrapper();
        requestInfoWrapper.setUserCode(userCode);
        requestInfoWrapper.setClientId(clientId);
        requestInfoWrapper.setComCode(comCode);
//        requestInfoWrapper.setIgnore(ignore);
        if (StringUtils.isBlank(requestInfoWrapper.getUserCode()) || StringUtils.isBlank(requestInfoWrapper.getClientId())) {
            throw new RuntimeException("还未登陆");
        }
        AddPowerVo addPowerVo = new AddPowerVo();
        addPowerVo.setUserCode(requestInfoWrapper.getUserCode());
//        todo需要設置
//        addPowerVo.setTaskCodes(Lists.newArrayList(SelfConstants.TaskCode.ORDER_QUERY));
//        addPowerVo.setAppKey(SelfConstants.APP_KEY);
        addPowerVo.setTableFieldAlis(Maps.newHashMap());
        //这是涉及到装箱和拆箱，所有需要一步到位，如果涉及装箱和拆箱则会损耗性能
        addPowerVo.setIsSql(Boolean.TRUE);
        addPowerVo.setIsEnum(Boolean.FALSE);
        requestInfoWrapper.setAddPowerVo(addPowerVo);
        return requestInfoWrapper;
    }

    @Override
    public String wrapperToPowerString(AddPowerVo addPowerVo, ApiPowerResult result) {
        return PowerUtil.wrapperToPowerString(addPowerVo, result);
    }

    @Override
    public AddPowerVo getAddPowerVoByKey(String key) {
        return null;
    }
}
